package com.hjt.gulimall.product.app;

import com.hjt.common.utils.PageUtils;
import com.hjt.common.utils.R;
import com.hjt.gulimall.product.entity.AttrVo;
import com.hjt.gulimall.product.entity.ProductAttrValueEntity;
import com.hjt.gulimall.product.service.AttrService;
import com.hjt.gulimall.product.service.ProductAttrValueService;
import com.hjt.gulimall.product.vo.AttrResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author huangjingtai
 * @email 970075985@qq.com
 * @date 2021-09-08 16:15:19
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;
    /**
     * 列表查询
     * @param catelogId
     * @param params
     * @return
     */
    @RequestMapping("/{attrType}/list/{catelogId}")
    public R baseAttrList(@PathVariable("catelogId") String catelogId,
                          @RequestParam Map<String, Object> params,
                          @PathVariable("attrType") String attrType) {
        PageUtils page =  attrService.queryBaseAttrPage(catelogId, params, attrType);
        return R.ok().put("page", page);
    }

    /**
     * 获取spu规格
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R listForSpu(@PathVariable("spuId") Long spuId) {

        List<ProductAttrValueEntity> list = productAttrValueService.listBySpuId(spuId);
        return R.ok().put("data",list);
    }

    /**
     * 修改spu的规格信息
     * @param spuId
     * @return
     */
    @PostMapping("update/{spuId}")
    public R updateBySpuId(@PathVariable Long spuId, @RequestBody List<ProductAttrValueEntity> attrValueEntities) {
        productAttrValueService.updateBatchBySpuId(spuId, attrValueEntities);
        return R.ok();
    }

//    /**
//     * 列表
//     */
//    @RequestMapping("/list")
//    //@RequiresPermissions("product:attr:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = attrService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }


    /**
     * 查询规格属性详细信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrResponseVo attrResponseVo =  attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attrResponseVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
		attrService.updateAttrInfo(attr);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));
        return R.ok();
    }

}
